using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using TheWorldLibrary.GameStates;
using TheWorld.GameScreens;
using TheWorldLibrary.Input;
using TheWorld.Components;
using TheWorld.Global;
using TheWorld.Networking;

namespace TheWorld
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Client : Game
    {
        #region XNA Field Region

        public static GraphicsDeviceManager graphics;
        public SpriteBatch SpriteBatch;

        #endregion

        #region Game State Region

        public static GameStateManager StateManager;
        public static MainMenuScreen MainMenuScreen;
        public LoginScreen LoginScreen;
        public RegisterScreen RegisterScreen;
        public static GamePlayScreen GamePlayScreen;
        public static CreateCharacterScreen CreateCharacterScreen;
        public static ChooseCharacterScreen ChooseCharacterScreen;

        #endregion

        #region Screen Field Region

        const int ScreenWidth = 1024;
        const int ScreenHeight = 768;

        public readonly Rectangle ScreenRectangle;

        #endregion

        #region Frames Per Second Field Region

        private float fps;
        private float updateInterval = 1.0f;
        private float timeSinceLastUpdate = 0.0f;
        private float frameCount = 0;

        #endregion

        // Game Time //
        public GameTime _gameTime;


        public Client()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            graphics.PreferredBackBufferWidth = ScreenWidth;
            graphics.PreferredBackBufferHeight = ScreenHeight;

            ScreenRectangle = new Rectangle(
                0,
                0,
                ScreenWidth,
                ScreenHeight);

            Components.Add(new InputHandler(this));

            StateManager = new GameStateManager(this);
            Components.Add(StateManager);

            MainMenuScreen = new MainMenuScreen(this, StateManager);
            LoginScreen = new LoginScreen(this, StateManager);
            RegisterScreen = new RegisterScreen(this, StateManager);
            GamePlayScreen = new GamePlayScreen(this, StateManager);
            CreateCharacterScreen = new CreateCharacterScreen(this, StateManager);
            ChooseCharacterScreen = new ChooseCharacterScreen(this, StateManager);

            StateManager.ChangeState(MainMenuScreen);

            IsMouseVisible = true;

            Static._gameTime = new GameTime();
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // Init ContentManager
            GameContentManager.Instance.initContentManager(Services);

            // Load Textures
            GfxManager.Instance.initTextures();

            // Init Network
            Network.Instance.InitNetwork();

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            SpriteBatch = new SpriteBatch(GraphicsDevice);

            DataManager.ReadEntityData();

            
            Mapping.Instance.LoadMap();

            //DataManager.ReadArmorData(Content);
            //DataManager.ReadShieldData(Content);
            //DataManager.ReadWeaponData(Content);

            //DataManager.ReadChestData(Content);
            //DataManager.ReadKeyData(Content);

            //DataManager.ReadSkillData(Content);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                Exit();

            Static._gameTime = gameTime;

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            base.Draw(gameTime);

            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
            frameCount++;
            timeSinceLastUpdate += elapsed;

            if (timeSinceLastUpdate > updateInterval)
            {
                fps = frameCount / timeSinceLastUpdate;
#if XBOX360
                System.Diagnostics.Debug.WriteLine("FPS: " + fps.ToString());
#else
                Window.Title = "FPS: " + fps;
#endif
                frameCount = 0;
                timeSinceLastUpdate -= updateInterval;
            }
        }
    }
}
